package OSMS.action.goodsfunc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import OSMS.dao.GoodsDao;
import OSMS.entity.Car;
import OSMS.entity.Customers;
import OSMS.entity.Goods;
import OSMS.entity.Order;
/*
 * 订单页面
 * 	业务逻辑如下：
 * 	1、通过request.getParameter("cid")首先从购物车页面（showCar.jsp）中获取用户编号(cid)并转化为int型
 * 	2、通过调用GoodsDao中的orderList（cid）：来查询order订单表中的商品数据
 * 	3、将该列表装到域对象request中
 * 	4、选择对应的跳转方式（请求转发）跳转到orderPage.jsp订单结算页面.
 */
public class orderServlet extends HttpServlet {
	private GoodsDao dao = new GoodsDao();
	
	
	public double sum_cost(int cid){
		List<Order> orderList = new ArrayList<Order>();
		try {
			orderList = (ArrayList<Order>)dao.orderList(cid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		double sum = 0;
		for(int i = 0;i<orderList.size();i++)
		{
			sum+= orderList.get(i).getGood_num()*orderList.get(i).getGprice();
		}
		return sum;
	}
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			//设置编码格式
			response.setContentType("text/html;charset=utf-8");
	        request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
			
	        //通过request.getParameter("cid")首先从购物车页面（showCar.jsp）中获取用户编号(cid)
			HttpSession session = request.getSession();
			Object obj_c = session.getAttribute("cus");
			Customers cus = (Customers)obj_c;
			int cid = cus.getCid();												//用户编号get √
			
			
			
			//获取jsp页面中复选框的值(即提交订单的商品编号)
			String gids[] = null;	
			gids = request.getParameterValues("checkboxs");
			
			
	
			boolean flag_addsale = false;			//增加商品销量的flag
			boolean flag_minusstore = false;		//减少商品库存的flag
			boolean flag_createOrder = false;		//创建商品订单的flag
			boolean flag_delete = false;			//删除购物车商品flag
			
			Goods tmp_good = new Goods();			//暂存遍历到的商品对象
			Car tmp_car = new Car();				//暂存遍历到的购物车中商品对象
			int good_num;							//暂存遍历到的购物车商品的商品数量
			//遍历选中的商品
			for(int i = 0;i< gids.length;i++)
			{
				//获取商品id
				int gid = Integer.parseInt(gids[i]);

				
				//获取该商品的商品对象
				tmp_good =  dao.getGoodsById(gid);
				
				//获取该商品在购物车中的对象
				tmp_car = dao.getCarByCusAndGid(cid, gid);
				
				//获取商品在购物车中的个数
				good_num = tmp_car.getGood_num();
				
				//增加商品销量
				flag_addsale = dao.addOneGoodSale(good_num, gid);
				
				//减少商品库存
				flag_minusstore = dao.minusOneGoodStore(good_num, gid);
				
				//订单表中插入该订单
				flag_createOrder = dao.addOneOrder(cus, tmp_car);
				
				//从购物车中删除该商品
				flag_delete = dao.deleteGoods(gid, cid);
			}
			
			
			//计算订单总金额
			double sum_cost = this.sum_cost(cid);
			session.setAttribute("sum_cost", sum_cost);
			System.out.println(sum_cost);
			
			// 1. 调用OrderDao的方法查询order数据
			List<Order> orderList = dao.orderList(cid);
			System.out.println("订单的cid："+orderList.get(0).getCid());
			// 2. 将orderList数据装入域对象中。
			request.setAttribute("orderList", orderList);
			// 3. 选择对应的跳转方式（请求转发）跳转到orderPage.jsp订单结算页面.
			request.getRequestDispatcher("orderPage.jsp").forward(request, response);
		} catch(Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
	
}
